#pragma once
#include <map>
using namespace std;

template<typename T>
class TMap {
public:
	typedef map<UINT,void*> MAP_T;
	typedef map<UINT,void*>::iterator ITER_T;

	TMap()
	{
		CS = new Critical();
	}

	~TMap()
	{
		if (CS->GetLockCount()==-1)
			RP(CS);
	}

	bool Add(UINT index,T t)
	{
		CS->Enter();
		pair<ITER_T,bool> InsertStatus = tMap.insert(MAP_T::value_type(index,t));
		CS->Leave();
		return InsertStatus.second;
	}

	void Del(UINT index)
	{
		CS->Enter();
		tMap.erase(index);
		CS->Leave();
	}

	T Get(UINT index)
	{
		T t = 0;
		CS->Enter();
		tIter = tMap.find(index);
		if (tIter!=tMap.end())
			t = (T)(tIter->second);
		CS->Leave();
		return t;
	}

	UINT Count()
	{
		CS->Enter();
		UINT count = (UINT)tMap.size();
		CS->Leave();
		return count;
	}
private:
	Critical *CS;
	MAP_T tMap;
	ITER_T tIter;
};